package com.bicon.botu

import com.bicon.botu.core.base.annotation.*
import com.cxytiandi.elasticjob.annotation.EnableElasticJob
import org.mybatis.spring.annotation.MapperScan
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.FilterType
import org.springframework.web.multipart.MultipartResolver
import org.springframework.web.multipart.commons.CommonsMultipartResolver

/**
 * 主模块启动类
 * @author tandk
 * @date 2018/7/17 10:21
 */
@SpringBootApplication(exclude = arrayOf(JpaRepositoriesAutoConfiguration::class))
@ComponentScan(basePackages = arrayOf("com.bicon.botu"), excludeFilters = arrayOf(ComponentScan.Filter(type = FilterType.REGEX, pattern = arrayOf(".*Application"))))
@EnableGlobalRedis
@EnableMyBatisPlusConfig
@MapperScan("com.bicon.botu.**.mapper")
// http请求日志
@EnableHttpLogAspect
// 统一异常处理
@EnableGlobalExceptionHandler
// 系统拦截器
@EnableSystemInterceptor
// 定时任务
@EnableElasticJob
open class BotuMainApplication

fun main(args: Array<String>) {
    SpringApplication.run(BotuMainApplication::class.java, *args)
}

/**
 * 文件写入硬盘前内存中保存的最大值
 */
@Value("\${max.memory.size}")
val max_memory_size: Int = 40960

/**
 * 上传文件大小50M 50*1024*1024
 */
@Value("\${max.upload.size}")
val max_upload_size: Long = 52428800

/**
 * 显示声明CommonsMultipartResolver为mutipartResolver
 */
@Bean(name = arrayOf("multipartResolver"))
fun multipartResolver(): MultipartResolver {
    val resolver = CommonsMultipartResolver()
    resolver.setDefaultEncoding("UTF-8")
    //resolveLazily属性启用是为了推迟文件解析，以在UploadAction中捕获文件大小异常
    resolver.setResolveLazily(true)
    resolver.setMaxInMemorySize(max_memory_size)
    resolver.setMaxUploadSize(max_upload_size)
    return resolver
}